package com.cly.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.base.contants.CacheKeysConstants;
import com.cly.dto.PostDto;
import com.cly.entity.Post;
import com.cly.mapper.PostMapper;
import com.cly.result.JsonRequest;
import com.cly.result.exenum.RequestException;
import com.cly.service.IPostService;
import com.cly.vo.PostVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 岗位表 服务实现类
 *
 * @author by 春风能解释
 * <p>
 * 2024/11/9
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    /**
     * 分页查询岗位
     *
     * @param current 页码
     * @param size    页长
     * @param post    查询条件
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.RecruitKeys.POST_ARRAY, key = "#root.args")
    public JsonRequest<List<PostVo>> lookPostList(Integer current, Integer size, PostDto post) {
        IPage<PostVo> postPages = baseMapper.selectPostPages(new Page<>(current, size), post);
        // 传入分页
        return JsonRequest.success(postPages.getRecords(), postPages.getTotal());
    }

    /**
     * 新增岗位
     *
     * @param post 新增数据
     * @return JSON
     */
    @Override
    @CacheEvict(value = {CacheKeysConstants.RecruitKeys.POST_ARRAY,
            CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_COUNT,
            CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_PIE,
            CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_LINE}, allEntries = true)
    public JsonRequest<Boolean> addOnePost(Post post) {
        return JsonRequest.success(save(post));
    }

    /**
     * 修改岗位
     *
     * @param post 修改数据
     * @return JSON
     */
    @Override
    @CacheEvict(value = {CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_PIE,
            CacheKeysConstants.RecruitKeys.POST_ARRAY}, allEntries = true)
    public JsonRequest<Boolean> updateOnePost(Post post) {
        Post post1 = getById(post.getId());
        if (Objects.isNull(post1)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        return JsonRequest.success(updateById(post));
    }

    /**
     * 删除岗位
     *
     * @param ids 岗位id
     * @return JSON
     */
    @Override
    @CacheEvict(value = {CacheKeysConstants.RecruitKeys.POST_ARRAY,
            CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_COUNT,
            CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_PIE,
            CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_LINE}, allEntries = true)
    public JsonRequest<Boolean> deletePost(List<Long> ids) {
        return JsonRequest.success(removeByIds(ids));
    }
}




